#include "filemessage.h"
#include "qlabel.h"
// #include"VideoPlayer.h"
#include "qqmlcontext.h"

#include <QFileDialog>
#include <QFileIconProvider>
#include <QFileInfo>
#include <QProcess>
#include <QQmlApplicationEngine>

FileMessage::FileMessage(const QString &p, const NewsType &FType, QWidget *parent)
    : QWidget{parent}
    , path(p)
    , n(FType)
{
    switch (FType){
    case NewsType::picture :
        picture();
        break;
    case NewsType::video:
        video();
        break;
    case NewsType::rests:
        rests();
        break;
    case NewsType::text:
        break;
    };
}

FileMessage::~FileMessage()
{
    if (engine != nullptr){
        engine->deleteLater();
    }
}

QSize FileMessage::sizeHint() const
{
    return sizeH;
}

void FileMessage::mousePressEvent(QMouseEvent *)
{
    switch (n){
    case NewsType::picture:{
        qDebug() << "图片";
        engine = new QQmlApplicationEngine;

        engine->rootContext()->setContextProperty("Url", "file:" + path);
        engine->load(QUrl(QStringLiteral("qrc:/qml/ShowPicture.qml")));
        }
        break;
    case NewsType::video: {
        qDebug() << "视频";
        // VideoPlayer vp("fds", this);
        // vp.init();
    }
        
        break;
    case NewsType::rests:
        qDebug() << "其他文件";
        //打开系统资源管理器
        QProcess::startDetached("explorer.exe", {".\\data\\other files"});
        break;
    case NewsType::text:
        break;
    }
}

void FileMessage::picture()
{
    QPixmap p(path);
    QLabel *l = new QLabel(this);
    l->setScaledContents(true);
    sizeH.setWidth((p.width() * 0.2) > 260 ? 260 : (p.width() * 0.2));
    sizeH.setHeight(((double)(double)sizeH.width() / (double)p.width()) * p.height());
    l->setFixedSize(sizeH);
    setFixedSize(sizeH);
    l->setPixmap(p);

    sizeH.setHeight(sizeH.height() + 10);
}

void FileMessage::video()
{
    QPixmap p(path);
    QLabel *l = new QLabel(this);
    l->setScaledContents(true);

    sizeH.setWidth((p.width() * 0.2) > 260 ? 260 : (p.width() * 0.2));
    sizeH.setHeight(((double)(double)sizeH.width() / (double)p.width()) * p.height());
    l->setFixedSize(sizeH);

    QLabel *la = new QLabel(this);
    la->setScaledContents(true);
    la->setFixedWidth(l->width() * 0.4);
    la->setFixedHeight(l->height() * 0.4);
    la->move((l->width() - la->width()) / 2, (l->height() - la->height()) / 2);
    la->setPixmap(QPixmap("E:\\0Learn_to_code\\Qt\\chatFrame\\play1.png"));

    setFixedSize(sizeH);
    l->setPixmap(p);

    sizeH.setHeight(sizeH.height() + 10);
}

void FileMessage::rests()
{
    // 使用 QFileInfo 来获取文件信息
    QFileInfo fileInfo(path);
    QFileIconProvider icon_provider;

    sizeH.setWidth(240);
    sizeH.setHeight(100);
    setFixedSize(sizeH);
    this->setStyleSheet("QWidget {background-color: rgb(255, 255, 255);}");

    QLabel *b = new QLabel(this);
    b->setFixedSize(sizeH);
    QLabel *text1 = new QLabel(this);
    text1->setGeometry(QRect(10,10,140,60));
    text1->setAlignment(Qt::AlignJustify);//对齐模式
    text1->setWordWrap(true);//文本自动换行
    text1->setText(fileInfo.fileName());
    QLabel *text2 = new QLabel(this);
    text2->setGeometry(QRect(15,75,100,30));
    text2->setAlignment(Qt::AlignJustify);//对齐模式
    text2->setText(QString::number(fileInfo.size() / 1024.0) + " KB");
    QLabel *picture = new QLabel(this);
    picture->setGeometry(QRect(165,20, 64,64));
    picture->setScaledContents(true);
    picture->setPixmap(icon_provider.icon(fileInfo).pixmap(QSize(64,64)));

    sizeH.setHeight(sizeH.height() + 10);
}
